#ifndef COLOR_H
#define COLOR_H

#include <stdint.h>
#include <endian.h>

/**
 * RGBA (Red, Green, Blue and Alpha) color structure.
 *
 * @note A (alpha) is unused so far, it's kept since operating on 32bits is
 * faster or equal than on 24bits.
 */
typedef struct color {
  uint32_t b:8, g:8, r:8, a:8;
} color_t;


#define COLOR_FROM_PAIR_ARGB( color, A, R, G, B ) ({                          \
  (color).a = (A); (color).r = (R), (color).g = (G); (color).b = (B); } )

#define COLOR_FROM_PAIR_RGB( color, R, G, B ) ({                              \
  (color).r = (R), (color).g = (G); (color).b = (B); } )

/**
 * Copy the first color (c1) over the second (c2).
 */
#define COLOR_COPY( c1, c2 )                                                  \
  COLOR_FROM_PAIR_ARGB( c2, (c1).a, (c1).r, (c1).g, (c1).b )

/**
 * Convert from a 32 bit unsigned integer in the RGBA convention,
 * like 0x00ffff00 for a yellow (R=255, G=255, B=0) with zero alpha
 */
#define COLOR_FROM_ARGB( color, rgba ) ({                                     \
  (color).a = ( (rgba) >> 24 ) & 0xff;                                        \
  (color).r = ( (rgba) >> 16 ) & 0xff;                                        \
  (color).g = ( (rgba) >> 8  ) & 0xff;                                        \
  (color).b = ( (rgba) >> 0  ) & 0xff;                                        \
  (color); /* "return" color */                                               \
})

/**
 * Return a 32bit unsigned integer in the RGBA convention.
 */
#define COLOR_TO_ARGB(color)                                                  \
  ( ((color).a << 24 ) | ((color).r << 16 ) | ((color).g << 8  ) | (color).b )


/* Define basic colors in ARGB format */
/* RGB */
#define COLOR_RED                                                    0x00ff0000
#define COLOR_GREEN                                                  0x0000ff00
#define COLOR_BLUE                                                   0x000000ff

/* CMY */
#define COLOR_CYAN                                                   0x0000ffff
#define COLOR_MAGENTA                                                0x00ff00ff
#define COLOR_YELLOW                                                 0x00ffff00

/* White, Black and Grays */
#define COLOR_BLACK                                                  0x00000000
#define COLOR_GRAY10                                                 0x001a1a1a
#define COLOR_GRAY20                                                 0x00333333
#define COLOR_GRAY30                                                 0x004d4d4d
#define COLOR_GRAY40                                                 0x00666666
#define COLOR_GRAY50                                                 0x007f7f7f
#define COLOR_GRAY60                                                 0x00999999
#define COLOR_GRAY70                                                 0x00b3b3b3
#define COLOR_GRAY80                                                 0x00cccccc
#define COLOR_GRAY90                                                 0x00e5e5e5
#define COLOR_WHITE                                                  0x00ffffff


#endif /* COLOR_H */
